//Abstract class
function Sprite(context, x, y, width, height, clickCallback, hoverCallback) {
	
	this.context = context;
	
	this.position = new Vector2(x, y);	
	
	this.width = width;
	this.height = height;

	this.image = document.createElement("canvas");			
	this.image.id = new Date().getTime();	
	this.imageContext = this.image.getContext("2d");
	this.image.width = this.width;
	this.image.height = this.height;		
	
	this.isHovered = false;
	this.isClicked = false;	
	
	this.update;
	
	this.clickCallback = clickCallback;
	this.hoverCallback = hoverCallback;	
	
	this.isRotated = false;
	this.radians = 0;					
}
	
Sprite.prototype.draw = function() {
	this.context.save();
	
	this.context.translate(this.position.x, this.position.y)		
	
	if(this.isRotated) {
		if(this.radians < 0) {												
			this.context.rotate(this.radians);
			this.context.translate(-this.image.width, 0);
		}
		else {										
			this.context.rotate(this.radians);
			this.context.translate(0, -this.image.height);
		}
	}
			
	this.context.drawImage(this.image, 0, 0);
						
	this.context.restore();				
}

Sprite.prototype.move = function(x, y) {
	if(arguments.length === 1) {
		this.position.move(x);
	}	
	else {
		this.position.move(x, y);
	}	
}	

Sprite.prototype.translate = function(x, y) {
	if(arguments.length === 1) {
		this.position.translate(x);
	}	
	else {
		this.position.translate(x, y);
	}	
}	

Sprite.prototype.isMouseInside = function(e) {

		this.context.save();
		this.context.translate(this.position.x, this.position.y);			
		
		if(this.isRotated) {			
			this.context.translate(0, this.image.width);							
			this.context.rotate(this.radians);
		}
	
		//this.context.translate(-this.position.x, -this.position.y);			
				
		this.context.beginPath();
		
		this.context.moveTo(0, 0);
		this.context.lineTo(this.width, 0);
		this.context.lineTo(this.width, this.height);
		this.context.lineTo(0, this.height);
		this.context.lineTo(0, 0);
		/*
		this.context.moveTo(this.x, this.y);
		this.context.lineTo(this.x + this.width, this.y);
		this.context.lineTo(this.x + this.width, this.y + this.height);
		this.context.lineTo(this.x, this.y + this.height);
		this.context.lineTo(this.x, this.y);
		this.context.closePath();
			*/
		var isInside =	this.context.isPointInPath(e.pageX, e.pageY)	
		
		this.context.restore();
											
		return isInside;	
}
			
Sprite.prototype.onclick = function(e) {
	if(this.isMouseInside(e)) {
		
		if(this.clickCallback !== null) {					
			this.clickCallback(this);
		}
	}
}

Sprite.prototype.onmousemove = function(e) {

	if(this.isMouseInside(e)) {
		
		this.setHovered(true);
					
		if(this.hoverCallback !== null) {
			this.hoverCallback(this);					
		}			
	}

	else {
		this.setHovered(false);
	}
}

Sprite.prototype.setHovered = function(isHovered) {
	if(this.isHovered !== isHovered) {
		this.isHovered = isHovered;
		this.initImage();
	}	
}

Sprite.prototype.rotate = function(radians) {
	this.radians = radians;
	this.isRotated = true;		
}